//tslint:disable
import { Component, OnInit, Input } from "@angular/core";

import { ModalController } from "@ionic/angular";

import { ActionSheetService } from "ng-zorro-antd-mobile";

import { RemarksContentComponent } from "../remarks-content/remarks-content.component";
import { AppGlobalService } from "../../shared/service/app-global.service";
import { HttpService } from "../../shared/service/http.service";
import { ImgSlidePage } from '../slides-imgage/img-slide.page';
import { utf8Encode } from "@angular/compiler/src/util";
import { PageModalService } from "src/app/shared/service/page-modal-service";

@Component({
  selector: "app-remarks",
  templateUrl: "./remarks.component.html",
  styleUrls: ["./remarks.component.scss"]
})
export class RemarksComponent implements OnInit {
  @Input() tagData: any;
  openRemarks = false;
  openRemarksList = false;
  remarksText = "";
  pageTotal: 0;
  pageSize: 0;
  pageNo: any = 1;
  params: any;

  constructor(
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    private httpService: HttpService,
    public modalController: ModalController
  ) { }

  ngOnInit() {
    this.params = this.tagData.data.submit;
    this.pageNo = this.tagData.data.pageNo;
    this.pageSize = this.tagData.data.pageSize;
    this.pageTotal = this.tagData.data.total;
  }

  // 业务备注->填写备注弹框
  async presentModal(value) {
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: value,
      component: RemarksContentComponent,
      componentProps: { tagData: this.tagData, item: this.tagData.data.titlebar.remarkIcon, type: value, pageSize: this.pageSize }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    console.log(modalData);
  }
  // 业务备注->备注列表弹框
  showActionSheet = () => {
    const buttons = this.tagData.data.titlebar.moreLinkIcon.formData.map(
      obj => obj.label
    );
    ActionSheetService.showActionSheetWithOptions(
      {
        options: buttons
      },
      buttonIndex => {
        console.log(buttonIndex);
        if (buttonIndex >= 0) {
          this.go(
            this.tagData.data.titlebar.moreLinkIcon.formData[buttonIndex]
          );
        }
      }
    );
  };

  addRemarks() {
    this.openRemarks = true;
  }

  hiddenRemarks() {
    this.openRemarks = false;
    this.openRemarksList = false;
  }

  addRemarksList() {
    this.openRemarksList = true;
  }

  /**
   * 跳转备注页面
   *
   * @memberof DetailComponent
   */
  go(item) {
    if (item.link && item.link.type === "url") {
      const url = item.link.url ? item.link.url : item.link.params.url;
      const title = item.link.params.urlTitle ?
        item.link.params.urlTitle : '';
      this.appGlobalService.openUrl(title, url);
    } else if (item.link && item.link.type === "contract") {
      const targetObj = { pageId: item.link.target, params: item.link.params, sessionId: localStorage.getItem('sessionId') };
      this.httpService.post('element/serviceContract/url', targetObj.params, res => {
        this.appGlobalService.openUrl(item.text, res.url, item.link.params.target, JSON.stringify(item.link.params));
      });
    } else if (item.link && item.link.type === "ljcontract") {
      const targetObj = { pageId: item.link.target, params: item.link.params, sessionId: localStorage.getItem('sessionId') };
      this.httpService.post('element/ljserviceContract/url', targetObj.params, res => {
        const title = item.text ? item.text : this.tagData.data.label;
        this.appGlobalService.openUrl(title, res.url, item.link.target, JSON.stringify(item.link.params));
      });
    } else if (item.button && item.button.submit) {
      const params = {
        content: item.button.submit.content,
        id: item.button.submit.id,
        sessionId: localStorage.getItem("sessionId")
      };
      this.httpService.post("application/submit", params, res => {
        this.appGlobalService.showToast(res.message, 1500, "middle");
        if (res.success) {
          this.appGlobalService.reloadCurrentPage();
        }
      });
    } else {
      this.pageModalService.openPage(item.link);
    }
  }

  /**
   * 提交备注
   */
  submitRemarks() {
    if (this.remarksText === "") {
      this.appGlobalService.showToast("请填写备注", 1000, "top");
      return;
    }
    const contentArray = this.tagData.data.titlebar.remarkIcon.formData.button
      .submit.content;
    contentArray["remark_text"] = this.remarksText;
    const params = {
      content: contentArray,
      id: this.tagData.data.titlebar.remarkIcon.formData.button.submit.id,
      sessionId: localStorage.getItem("sessionId")
    };
    this.httpService.post("application/submit", params, res => {
      if (res.success) {
        this.openRemarks = false;
        this.remarksText = "";
        this.tagData.data.data.push(res.data);
        this.appGlobalService.showToast(res.message, 1000, "bottom");
      } else {
        this.appGlobalService.showToast(res.message, 1000, "bottom");
      }
    });
  }

  loadMoreData(event) {
    setTimeout(() => {
      this.params.sessionId = localStorage.getItem("sessionId");
      // 请求下一个页面
      this.pageNo = this.pageNo + 1;
      this.params.content.pageNo = this.pageNo;
      this.params.content.pageSize = this.pageSize;
      // 请求获取新数据
      this.httpService.post("application/submit", this.params, res => {
        // 请求数据成功
        if (res.success) {
          // 追加返回数据
          if (Array.isArray(res.data.data) && res.data.data.length > 0) {
            // 追加数据
            res.data.data.map(item => {
              this.tagData.data.data.push(item);
            });
            // this.params = res.data.submit;
          } else {
            event.target.disabled = true;
            // 没有数据了
            // this.appGlobalService.showToast("没有数据了...", 1000, "bottom");
          }
          event.target.complete();
        } else {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, "middle");
          event.target.complete();
        }
      });
    }, 500);
  }

  async openImage(index, images) {
    const value = { img: images, index };
    const modal = await this.modalController.create({
      component: ImgSlidePage,
      componentProps: { value: JSON.stringify(value) }
    });
    return await modal.present();
  }


  normalBtnClick(btnItem) {

  }
}
